<template>
  <div class="login-modal-mask">
    <div class="login-modal">
      <span class="close-btn" @click="$emit('close')">&times;</span>
      <h2 class="login-title">登录</h2>
      <form class="login-form" @submit.prevent="onLogin">
        <label class="login-label">用户名</label>
        <input
          v-model="username"
          class="login-input"
          type="text"
          placeholder="请输入用户名"
          required
        />
        <label class="login-label">密码</label>
        <input
          v-model="password"
          class="login-input"
          type="password"
          placeholder="请输入密码"
          required
        />
        <button
          class="login-btn"
          type="submit"
          :disabled="isLoading"
          :class="{ loading: isLoading }"
        >
          {{ isLoading ? "登录中..." : "登录" }}
        </button>
      </form>
      <div class="login-footer">
        还没有账号？
        <a class="register-link" @click="$emit('register')">立即注册</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";
import { login as apiLogin } from "../api/user";

const isLoading = ref(false);
const router = useRouter();
const store = useStore();
const emit = defineEmits(["close"]);
const username = ref("");
const password = ref("");

const onLogin = async () => {
  // 表单验证
  if (!username.value.trim()) {
    ElMessage.warning("请输入用户名");
    return;
  }
  if (!password.value.trim()) {
    ElMessage.warning("请输入密码");
    return;
  }
  isLoading.value = true;
  try {
    // 直接使用Vuex的login action处理登录逻辑
    const result = await store.dispatch("user/login", {
      username: username.value.trim(),
      password: password.value.trim(),
    });

    if (result.success) {
      ElMessage.success("登录成功");
      // 触发登录成功事件，传递用户信息
      const userInfo = store.state.user.user;
      emit("login-success", userInfo);
      // 根据用户类型进行跳转
      router.push(result.redirectPath);
      // 关闭登录弹窗
      emit("close");
    }
  } catch (error) {
    console.error("登录失败:", error);
    const errorMsg =
      error.response?.data?.msg || error.message || "登录失败，请重试";
    ElMessage.error(errorMsg);
  } finally {
    isLoading.value = false;
  }
};
</script>

<style scoped>
.login-modal-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.18);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-modal {
  position: relative;
  background: #fff;
  border-radius: 16px;
  padding: 38px 38px 32px 38px;
  max-width: 440px;
  width: 100%;
  box-shadow: 0 4px 32px rgba(140, 38, 230, 0.1);
  min-width: 320px;
  animation: popup-fade-in 0.2s;
}
@keyframes popup-fade-in {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
.close-btn {
  position: absolute;
  right: 18px;
  top: 18px;
  font-size: 2rem;
  color: #444;
  cursor: pointer;
  z-index: 10;
  font-weight: bold;
  background: #fff;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  line-height: 36px;
  text-align: center;
  transition: background 0.2s, color 0.2s;
  box-shadow: 0 2px 8px #e0e0e0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.close-btn:hover {
  background: #8d26e6;
  color: #fff;
}
.login-title {
  color: #8d26e6;
  font-size: 2rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 28px;
  letter-spacing: 1px;
}
.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-label {
  font-size: 1.08rem;
  color: #444;
  margin-bottom: 4px;
  margin-top: 10px;
}
.login-input {
  width: 100%;
  height: 48px;
  border: 1.5px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 14px;
  font-size: 1.1rem;
  color: #444;
  background: #fafaff;
  margin-bottom: 6px;
  transition: border 0.2s;
}
.login-input:focus {
  border: 1.5px solid #8d26e6;
  outline: none;
}
.login-btn {
  margin-top: 18px;
  width: 100%;
  height: 56px;
  background: #8d26e6;
  color: #fff;
  font-size: 1.35rem;
  font-weight: 700;
  border: none;
  border-radius: 28px;
  cursor: pointer;
  transition: background 0.2s;
  box-shadow: 0 2px 12px #e0e0e0;
  letter-spacing: 2px;
}
.login-btn:hover {
  background: #6d1bb3;
}
.login-footer {
  margin-top: 22px;
  text-align: center;
  color: #444;
  font-size: 1.08rem;
}
.register-link {
  color: #8d26e6;
  margin-left: 8px;
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}
.register-link:hover {
  color: #6d1bb3;
  text-decoration: underline;
}
@media (max-width: 600px) {
  .login-modal {
    padding: 18px 4vw 18px 4vw;
    max-width: 98vw;
    min-width: 0;
  }
  .login-title {
    font-size: 1.3rem;
  }
}
</style>
